import 'package:base_lib/common/bloc/base_bloc.dart';
import 'package:base_lib/index.dart';
import 'package:doctor_prescribe_lib/doctor_prescribe_component.dart';
import 'package:doctor_prescribe_lib/repository/http.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:rxdart/rxdart.dart';
import '../medical_search/entity/medical_search_entity.dart' as DrugSearchBean;

class MedicalSearchBloc extends BaseBloc {
  @override
  void dispose() {
    listSubject?.close();
  }

  @override
  void initState() {
    super.initState();
  }

  List<DrugSearchBean.Datas> list = [];
  PublishSubject<List<DrugSearchBean.Datas>> listSubject = PublishSubject();

  String orgID;
  int orgTypeID;

  void doInit(context, int orgTypeID, String orgID) {
    this.orgTypeID = orgTypeID;
    this.orgID = orgID;
    doSearch(context);
  }

  doSearch(context, {String searchKey = ''}) async {
    var result =
        await PrescribeHttp.searchMedicine(searchKey, orgTypeID, orgID);
    if (result.isSuccess) {
      list
        ..clear()
        ..addAll(result.data);
      listSubject.add(list);
    } else {
      ToastUtil.error(result.error.msg);
    }
  }

  ///进入编辑页
  void go2EditPage(BuildContext context, DrugSearchBean.Datas item) {
    Routers.navigateTo(context, DoctorPrescribeComponent.medicalEditPage,
            parameters: Parameters()..putObj('item', item))
        .then((value) {
      if (value == true) {
        Routers.pop(context, true);
      }
    });
  }
}
